package com.huitone.smspfm.core.filter;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.huitone.smspfm.logger.ExtLogger;
import org.springframework.data.redis.connection.RedisConnection;

import com.bin.common.utils.DateTimeUtils;
import com.bin.common.utils.StringUtils;
import com.huitone.smspfm.core.config.AppStaticConfigs;
import com.huitone.smspfm.core.constant.DealResult;
import com.huitone.smspfm.redis.model.AppCanstant;
import com.huitone.smspfm.redis.model.SmsEntity;
import com.huitone.smspfm.redis.pipeline.PipelineOpts.ABExcuteCommands;

/**   
* @Title ForbidTimeFilterTask.java 
* @Package com.huitone.smspfm.core.task 
* @Description TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年6月18日 上午10:47:11 
* @version V1.0   
*/
public class ForbidTimeFilterTask2 extends Thread {
	
	private ExtLogger logger = ExtLogger.create(ForbidTimeFilterTask2.class);
	
	private String moduleName = "短信禁发时段过滤模块";
	
	private byte[] moduleNameB = AppStaticConfigs.stringRedisSerializer.serialize(moduleName); 
	
	private List<SmsEntity>[] results = null;
	
	private List<SmsEntity> entities = null;
	
	private int index = 0;

	public ForbidTimeFilterTask2(List<SmsEntity> entities, List<SmsEntity>[] results, int index) {
		this.entities = entities;
		this.results = results;
		this.index = index;
	}

	@Override
	public void run() {
		
		if (entities == null) return;
		
		long start = System.currentTimeMillis();
		
		AppStaticConfigs.pipelineOpts.save(new ABExcuteCommands() {

			@Override
			public void excute(RedisConnection connection) {
				
				List<SmsEntity> lists = new ArrayList<>();
				
				for (SmsEntity entity:entities) {
					
					if (entity.isExists()) {
						
//						SMTempSet tempSet = new SMTempSet();
//						tempSet.setSerialNo(entity.getSequence());
//						tempSet.setUserNumber(entity.getUser());
//						tempSet.setDataType(SMTempSet.DataType.submit.toString());
//						tempSet.setDealStep(SMTempSet.DealStep.sensitive.toString());
//						tempSet.setDealModule(moduleName);
						
						Map<byte[], byte[]> hashMap = new HashMap<>();
						hashMap.put(AppCanstant.SmsEntity.DealModuleName, moduleNameB);
						hashMap.put(AppCanstant.SmsEntity.SdealTime, AppStaticConfigs.stringRedisSerializer.serialize(DateTimeUtils.dateToStr2(new Date())));
						hashMap.put(AppCanstant.SmsEntity.ConSm, AppStaticConfigs.stringRedisSerializer.serialize(StringUtils.toString(StringUtils.strToInt(entity.getConSm()))));
						
						String value = entity.getSequence() + ":" + entity.getUser();
						String smInfo = "SMInfo:" + value;
						String srvNode = StringUtils.toString(entity.getSrvNode());
						String serviceCode = StringUtils.toString(entity.getServiceCode());
						
						Set<String> set = new HashSet<>(); // 所有适用的规则
						
						Set<String> set2 = AppStaticConfigs.configForbidTimeMap.get(srvNode + ":" + serviceCode);
						if (set2 != null) {
							set.addAll(set2);
						}
						Set<String> set3 = AppStaticConfigs.configForbidTimeMap.get(srvNode + ":");
						if (set3 != null) {
							set.addAll(set3);
						}
						Set<String> set4 = AppStaticConfigs.configForbidTimeMap.get(":" + serviceCode);
						if (set4 != null) {
							set.addAll(set4);
						}
						Set<String> set5 = AppStaticConfigs.configForbidTimeMap.get(":");
						if (set5 != null) {
							set.addAll(set5);
						}
						
						List<String> forbidTime = null;
						for (String str:set) {
							if (StringUtils.isEmpty(str)) continue;
							String[] strings = str.split(":");
							float startTime = StringUtils.strToFlaot(StringUtils.toString(strings[2]));
							float endTime = StringUtils.strToFlaot(StringUtils.toString(strings[3]));
							if (DateTimeUtils.isInInterval(startTime, endTime)) {
								//序列号：手机号码: start-end,start-end
								if (forbidTime == null) {
									forbidTime = new ArrayList<>();
								}
								forbidTime.add(startTime + "-" + endTime);
							}
						}
						
						if (forbidTime != null) { // 写入SMForbidTList集合
							
							Date endTime = new Date();
							
							String zsetitemTmp = "";
							
							for (String fb:forbidTime) {
								zsetitemTmp += fb + ",";
							}
							
							String zsetitem = zsetitemTmp.substring(0, zsetitemTmp.lastIndexOf(","));
							
//							tempSet.setDealTime(endTime);
//							tempSet.setDealStatus(DealResult.RESULT0);
							
							hashMap.put(AppCanstant.SmsEntity.EdealTime, AppStaticConfigs.stringRedisSerializer.serialize(DateTimeUtils.dateToStr2(endTime)));
							hashMap.put(AppCanstant.SmsEntity.DealResult, DealResult.RESULT_1B);
							hashMap.put(AppCanstant.SmsEntity.DealResultDesc, DealResult.RESULT_1_DESCB);
							
//							connection.sAdd(AppStaticConfigs.iSMTempSetB,
//									AppStaticConfigs.stringRedisSerializer.serialize(tempSet.toString()));
							
							connection.hMSet(AppStaticConfigs.stringRedisSerializer.serialize(smInfo), hashMap);
							
							connection.zAdd(AppStaticConfigs.iSMForbidZSetB, DateTimeUtils.getHour(endTime), AppStaticConfigs.stringRedisSerializer.serialize(value + ":" + zsetitem));
							
							logger.warn("[序列号:用户号码][" + entity.getSequence() + ":" + entity.getUser() + "]被禁发时段检测过滤器捕获,短信序列被保存到禁发时段列表,处理流程结束。");
					    	
						} else {
							
							Date endTime = new Date();
							
//							tempSet.setDealTime(endTime);
//							tempSet.setDealStatus(DealResult.RESULT0);
							
							hashMap.put(AppCanstant.SmsEntity.EdealTime, AppStaticConfigs.stringRedisSerializer.serialize(DateTimeUtils.dateToStr2(endTime)));
							hashMap.put(AppCanstant.SmsEntity.DealResult, DealResult.RESULT0B);
							hashMap.put(AppCanstant.SmsEntity.DealResultDesc, DealResult.RESULT0_DESCB);
							
//							connection.sAdd(AppStaticConfigs.iSMTempSetB,
//									AppStaticConfigs.stringRedisSerializer.serialize(tempSet.toString()));
							
							connection.hMSet(AppStaticConfigs.stringRedisSerializer.serialize(smInfo), hashMap);
							
							lists.add(entity);
						}
						
					}
				}
				results[index] = lists;
			}
		});
		
		long end = System.currentTimeMillis();
		
		logger.debug("禁发时段过滤:" + (end - start) + "毫秒");
	}

}
